<?php
class News extends Vfriend_Db {	
	var $cache;
	var $clear;
	var $nlogs;
	var $notify;
	var $behaviour;
	public function __construct() {
		$this->cache 		= new Vfriend_Cache();
		$this->clear 		= $this->cache->clear;
		$this->nlogs 		= new Nlogs();		
		$this->notify		= new Notification();
		$this->behaviour	= new Behaviour();
	}
	public function add($intUserId, $strImage, $strTitle, $strContent, $strShort, $strTags, $intCategoryId, $intSubCategoryId, $strSrcLink) {
		try {				
			$this->prepare('call news_add(?,?,?,?,?,?,?,?,?)');
			$this->bindParam(1, $intUserId, 		PDO::PARAM_INT, 11);
			$this->bindParam(2, $intCategoryId, 	PDO::PARAM_INT, 11);
			$this->bindParam(3, $intSubCategoryId, 	PDO::PARAM_INT, 11);
			$this->bindParam(4, $strTitle, 			PDO::PARAM_STR, 255);
			$this->bindParam(5, $strContent, 		PDO::PARAM_STR);
			$this->bindParam(6, $strShort, 			PDO::PARAM_STR, 500);
			$this->bindParam(7, $strImage, 			PDO::PARAM_STR, 150);			
			$this->bindParam(8, $strTags, 			PDO::PARAM_STR, 100);				
			$this->bindParam(9, $strSrcLink, 		PDO::PARAM_STR, 150);				
			$arrResult 	= $this->fetchAll();			
			$intNewsId  = isset($arrResult[0]['newid']) ? $arrResult[0]['newid'] : 0;
			if($intNewsId) {	
				$this->clear->deleteNews($intSubCategoryId, $intUserId);
				$objVfriendUser = new Vfriend_User();
				$arrRelation	= $objVfriendUser->getRelation($intUserId,2);
				if($arrRelation) {					
					foreach($arrRelation as $value) {						
						$this->logBehaviour($value,$intUserId,$intUserId,51,$intNewsId);
					}
				}
				$this->logBehaviour($intUserId,$intUserId,$intUserId,51,$intNewsId);
			}
			return $intNewsId;
		} catch(Exception $e) {
			Vfriend_Function::logDb('News::add - ' .$e->getMessage());			
			return null;
		}
	}
	public function update($intUserId, $intNewsId, $strImage, $strTitle, $strContent, $strShort, $strTags, $intCategoryId, $intSubCategoryId,$strSrcLink) {
		try {			
			$this->prepare('call news_update(?,?,?,?,?,?,?,?,?,?)');
			$this->bindParam(1, $intUserId, PDO::PARAM_INT, 11);
			$this->bindParam(2, $intNewsId, PDO::PARAM_INT, 11);
			$this->bindParam(3, $intCategoryId, PDO::PARAM_INT, 11);
			$this->bindParam(4, $intSubCategoryId, PDO::PARAM_INT, 11);
			$this->bindParam(5, $strTitle, PDO::PARAM_STR, 255);
			$this->bindParam(6, $strContent, PDO::PARAM_STR);
			$this->bindParam(7, $strShort, PDO::PARAM_STR, 500);
			$this->bindParam(8, $strImage, PDO::PARAM_STR, 150);			
			$this->bindParam(9, $strTags, PDO::PARAM_STR, 100);			
			$this->bindParam(10, $strSrcLink, PDO::PARAM_STR, 150);			
			$arrResult 	= $this->fetchAll();
			$intError  	= isset($arrResult[0]['errCode']) ? $arrResult[0]['errCode'] : 0;	
			Vfriend_Function::logDb('News::edit - ' .$intError);			
			if($intError == 0) {
				$this->clear->deleteNews($intSubCategoryId, $intUserId);
				$this->clear->deleteNewsById($intNewsId);
			}
			return $intError;
		} catch(Exception $e) {
			Vfriend_Function::logDb('News::update - ' .$e->getMessage());			
			return null;
		}
	}
	public function getRead($intNewsId) {
		$strKey	 	= NEWS_UPDATEREAD . $intNewsId;
		$arrResult	= $this->cache->get($strKey); 
		return isset($arrResult['read']) ? $arrResult['read'] : 0;
	}
	public function updateRead($intNewsId) {
		try {	
			$isUpdate	= 0;
			$strKey	 	= NEWS_UPDATEREAD . $intNewsId;
			$arrResult	= $this->cache->get($strKey); 
			$intRead 	= 1;				
			if(!Vfriend_Function::isCached($arrResult)) {
				$isUpdate	= 1;
			} else {
				$timeLastUpdate 	= $arrResult['lastupdate'];
				$timeCurrent		= time();
				$intRead			= $arrResult['read']+1;
				if($timeLastUpdate+TIME_NEWS_UPDATE_READ >= $timeCurrent) {
					$isUpdate = 1;
				} else {					
					$arrUpdate	= array('lastupdate'=>$timeLastUpdate,'read'=>$intRead);
					$this->cache->set($strKey, $arrUpdate, NEWS_UPDATEREAD_EXPIRE);
				}
			}			
			if($isUpdate) {				
				$this->prepare('call news_update_tread(?,?)');
				$this->bindParam(1, $intNewsId, PDO::PARAM_INT, 11);
				$this->bindParam(2, $intRead, PDO::PARAM_INT, 6);
				$arrResult 	= $this->fetchAll();
				$intRead	= isset($arrResult['0']['tread']) ? $arrResult['0']['tread'] : $intRead;
				$arrUpdate	= array('lastupdate'=>time(),'read'=>$intRead);
				$this->cache->set($strKey, $arrUpdate, NEWS_UPDATEREAD_EXPIRE);
			}
			return $intRead;
			
		} catch(Exception $e) {
			Vfriend_Function::logDb('News::updateRead - ' .$e->getMessage());			
			return null;
		}
	}	
	public function delete($intUserId, $intNewsId) {
		try {			
			$this->prepare('call news_delete(?,?)');
			$this->bindParam(1, $intUserId, PDO::PARAM_INT, 11);
			$this->bindParam(2, $intNewsId, PDO::PARAM_INT, 11);;			
			$arrResult 	= $this->fetchAll();			
			$intError  	= isset($arrResult[0]['errCode']) ? $arrResult[0]['errCode'] : 0;
			if($intError == 0) {
				$intSubCategoryId = isset($arrResult[0]['subcategoryid']) ? $arrResult[0]['subcategoryid'] : 0;
				$this->clear->deleteNews($intSubCategoryId,$intUserId);
				$this->clear->deleteNewsById($intNewsId);
				$this->nlogs->deleteByUserId($intUserId, $intNewsId);
				$this->notify->deleteByType(63, $intNewsId);
				$this->notify->deleteByType(64, $intNewsId);
				$this->behaviour->deleteBySrcId(5,$intNewsId);
			}
			return $intError;
		} catch(Exception $e) {
			Vfriend_Function::logDb('News::delete - ' .$e->getMessage());			
			return null;
		}
	}
	public function get($intNewsId) {
		try {
			$strKey 	= NEWS_GET . $intNewsId;
			$arrResult	= $this->cache->get($strKey);
			if(!Vfriend_Function::isCached($arrResult)) {
				$this->prepare('call news_get(?)');
				$this->bindParam(1, $intNewsId, PDO::PARAM_INT, 11);
				$arrResult = $this->fetchAll();
				$this->cache->set($strKey, $arrResult, NEWS_GET_EXPIRE);
			}
			return isset($arrResult[0]) ? $arrResult[0] : null;
		} catch(Exeception $e) {
			Vfriend_Function::logDb('News::get - ' .$e->getMessage());			
			return null;
		}
	}
	public function getRss() {
		try {
			$strKey 	= NEWS_GETRSS;
			$arrResult	= $this->cache->get($strKey);
			if(!Vfriend_Function::isCached($arrResult)) {
				$this->prepare('call news_getall_rss()');
				$arrResult = $this->fetchAll();
				$this->cache->set($strKey, $arrResult, NEWS_GETRSS_EXPIRE);
			}
			return isset($arrResult) ? $arrResult : null;
		} catch(Exeception $e) {
			Vfriend_Function::logDb('News::getRss - ' .$e->getMessage());			
			return null;
		}
	}
	public function getAllForDashboard($intType) {
		try {
			$strKey 	= NEWS_GETALLFORDASHBOARD . $intType;
			$arrResult	= $this->cache->get($strKey);
			if(!Vfriend_Function::isCached($arrResult)) {
				$this->prepare('call news_getall_dashboard(?)');
				$this->bindParam(1, $intType, PDO::PARAM_INT, 4);
				$arrResult = $this->fetchAll();
				$this->cache->set($strKey, $arrResult, NEWS_GETALLFORDASHBOARD_EXPIRE);
			}
			return isset($arrResult) ? $arrResult : null;
		} catch(Exeception $e) {
			Vfriend_Function::logDb('News::getAllForDashboard - ' .$e->getMessage());			
			return null;
		}
	}
	public function getAll($intSubcategoryId=0, $strTitle, $intId, $intFilter=0, $intOrder=0, $intOff, $intLimit, &$intTotal=0) {
		try {				
			$arrResult	= null;
			$strKey		= '' ;
			if($intOff == 0 && $strTitle == '' && $intId == 0) {
				$strKey		= NEWS_GETALL . $intSubcategoryId . $intFilter . $intOrder;
				$arrResult	= $this->cache->get($strKey);
			}
			if(!Vfriend_Function::isCached($arrResult)) {
				$this->prepare('call news_getall(?,?,?,?,?,?,?)');
				$this->bindParam(1, $intSubcategoryId, PDO::PARAM_INT, 11);	
				$this->bindParam(2, $strTitle, PDO::PARAM_STR, 255);			
				$this->bindParam(3, $intId, PDO::PARAM_INT, 11);			
				$this->bindParam(4, $intFilter, PDO::PARAM_INT, 4);			
				$this->bindParam(5, $intOrder, PDO::PARAM_INT, 4);			
				$this->bindParam(6, $intOff, PDO::PARAM_INT, 11);
				$this->bindParam(7, $intLimit, PDO::PARAM_INT, 4);		
				$arrResult 	= $this->fetchAll();
				if($strKey != '') {
					$this->cache->set($strKey, $arrResult, NEWS_GETALL_EXPIRE);
				}
			}
			$intTotal	= isset($arrResult[0]['foundRows']) ? $arrResult[0]['foundRows'] : 0;
			return isset($arrResult) ? $arrResult : null;
		} catch(Exception $e) {
			Vfriend_Function::logDb('News::getAll - ' .$e->getMessage());			
			return null;
		}
	}
	public function getAllByUser($intUserId, $strStart, $strEnd, $strValue, $intOff, $intLimit, &$intTotal=0) {
		try {	
			$arrResult	= null;
			if($intOff == 0 && $strValue == '' && $strStart == '' && $strEnd == '') {
				$strKey 	= NEWS_GETALLBYUSER . $intUserId;						
				$arrResult = $this->cache->get($strKey);
			}
			if(!Vfriend_Function::isCached($arrResult)) {
				$this->prepare('call blogs_getall_user(?,?,?,?,?,?)');
				$this->bindParam(1, $intUserId, PDO::PARAM_INT, 11);
				$this->bindParam(2, $strStart, PDO::PARAM_STR, 10);
				$this->bindParam(3, $strEnd, PDO::PARAM_STR, 10);
				$this->bindParam(4, $strValue, PDO::PARAM_STR);
				$this->bindParam(5, $intOff, PDO::PARAM_INT, 11);
				$this->bindParam(6, $intLimit, PDO::PARAM_INT, 4);		
				$arrResult 	= $this->fetchAll();
				if($intOff == 0 && $strValue == '' && $strStart == '' && $strEnd == '') {
					$this->cache->set($strKey, $arrResult, NEWS_GETALLBYUSER_EXPIRE);
				}
			}
			$intTotal	= isset($arrResult[0]['foundRows']) ? $arrResult[0]['foundRows'] : 0;
			return isset($arrResult) ? $arrResult : null;
		} catch(Exception $e) {
			Vfriend_Function::logDb('News::getAllByUser - ' .$e->getMessage());			
			return null;
		}
	}
	public function getSearch($intFilter, $strKeyword, $strSubcategoryId, $strDateFrom, $strDateTo, $intOff, $intLimit, &$intTotal=0) {
		try {							
			$this->prepare('call news_search(?,?,?,?,?,?,?)');
			$this->bindParam(1, $intFilter, PDO::PARAM_INT, 11);
			$this->bindParam(2, $strKeyword, PDO::PARAM_STR, 50);
			$this->bindParam(3, $strSubcategoryId, PDO::PARAM_STR);
			$this->bindParam(4, $strDateFrom, PDO::PARAM_STR, 10);
			$this->bindParam(5, $strDateTo, PDO::PARAM_STR, 10);
			$this->bindParam(6, $intOff, PDO::PARAM_INT, 11);
			$this->bindParam(7, $intLimit, PDO::PARAM_INT, 4);
			$arrResult 	= $this->fetchAll();
			$intTotal	= isset($arrResult[0]['foundRows']) ? $arrResult[0]['foundRows'] : 0;
			return isset($arrResult) ? $arrResult : null;
		} catch(Exception $e) {
			Vfriend_Function::logDb('News::getSearch - ' .$e->getMessage());			
			return null;
		}
	}
	public function getSmartSearch($strKeyword,$intNewsId=0) {
		try {				
			$this->prepare('call news_getall_smartsearch(?,?)');
			$this->bindParam(1, $strKeyword, PDO::PARAM_STR, 255);
			$this->bindParam(2, $intNewsId, PDO::PARAM_INT, 11);			
			$arrResult 	= $this->fetchAll();
			return isset($arrResult) ? $arrResult : null;
		} catch(Exception $e) {
			Vfriend_Function::logDb('News::getSmartSearch - ' .$e->getMessage());			
			return null;
		}
	}
	public function plusLnews($intUserId, $intNewsId,&$intLike=0) {
		try {
			$this->prepare('call nlplus_add(?,?)');
			$this->bindParam(1, $intUserId, PDO::PARAM_INT, 11);
			$this->bindParam(2, $intNewsId, PDO::PARAM_INT, 11);			
			$arrResult 	= $this->fetchAll();
			$intError	= isset($arrResult[0]['errCode']) ? $arrResult[0]['errCode'] : null;
			if($intError == 0) {
				$intOwnerId = isset($arrResult[0]['userid']) ? $arrResult[0]['userid'] : 0;
				$intLike 	= isset($arrResult[0]['tlnews']) ? $arrResult[0]['tlnews'] : 0;
				$this->clear->deleteNewsById($intNewsId);
				$this->clear->deleteUstat($intUserId);
				$this->clear->deleteUstat($intOwnerId);
				# ---- LOGS ---- #
				$arrUFollow = $this->getFollowByNewId($intNewsId);
				if($arrUFollow) {
					$arrInfo	= $this->get($intNewsId);
					$strContent = Zend_Json::encode(array(
														'newid'  	=> $intNewsId,
														'title'  	=> $arrInfo['title'],
														'posterid' 	=> $intUserId
													)
												);
					foreach($arrUFollow as $value) {
						$intULogId = $value['userid'];
						if($intULogId != $intUserId) {
							$this->nlogs->insert($intULogId, 2, $intNewsId, $intNewsId);
							$this->notify->insert($intULogId, 64, $strContent, $intNewsId);
						}
					}
				}
			}
			return $intError;
		} catch(Exception $e) {
			Vfriend_Function::logDb('News::plusLnews - ' .$e->getMessage());			
			return null;
		}
	}
	public function getTopCmt($intNewId,$intLimit=BEHAVIOUR_TOP_LIMIT) {
		try {	
			$strKey	 	= NEWS_GETTOPCMT . $intNewId;
			$arrResult	= $this->cache->get($strKey);
			if(!Vfriend_Function::isCached($arrResult)) {
				$this->prepare('call ncomments_get_top(?,?)');
				$this->bindParam(1, $intNewId, PDO::PARAM_INT, 11);	
				$this->bindParam(2, $intLimit, PDO::PARAM_INT, 4);	
				$arrResult 	= $this->fetchAll();
				$this->cache->set($strKey, $arrResult, NEWS_GETTOPCMT_EXPIRE);
			}
			return isset($arrResult) ? $arrResult : null;
		} catch(Exception $e) {
			Vfriend_Function::logDb('News::getTopCmt - ' .$e->getMessage());			
			return null;
		}
	}
	public function getCmtUser($intNewsId) {
		try {	
			$strKey	 	= NEWS_GETCMTUSER . $intNewsId;
			$arrResult 	= $this->cache->get($strKey);
			if(!Vfriend_Function::isCached($arrResult)) {
				$this->prepare('call ncomments_get_user(?)');
				$this->bindParam(1, $intNewsId, PDO::PARAM_INT, 11);	
				$arrResult 	= $this->fetchAll();
				$this->cache->set($strKey, $arrResult, NEWS_GETCMTUSER_EXPIRE);
			}
			return $arrResult;
		} catch(Exception $e) {
			Vfriend_Function::logDb('News::getCmtUser - ' .$e->getMessage());			
			return null;
		}
	}
	public function getCmtTotal($intNewsId) {
		try {	
			$strKey	   = NEWS_GETCMTTOTAL . $intNewsId;
			$arrResult = $this->cache->get($strKey);
			if(!Vfriend_Function::isCached($arrResult)) {
				$this->prepare('call ncomments_get_total(?)');
				$this->bindParam(1, $intNewsId, PDO::PARAM_INT, 11);	
				$arrResult 	= $this->fetchAll();
				$this->cache->set($strKey, $arrResult, NEWS_GETCMTTOTAL_EXPIRE);
			}
			$intTotal	= isset($arrResult[0]['foundRows']) ? $arrResult[0]['foundRows'] : 0;
			return $intTotal;
		} catch(Exception $e) {
			Vfriend_Function::logDb('News::getCmtTotal - ' .$e->getMessage());			
			return null;
		}
	}
	public function getCmtList($intNewsId, $intOffset, $intLimit) {
		try {	
			$this->prepare('call ncomments_getall(?,?,?)');
			$this->bindParam(1, $intNewsId, PDO::PARAM_INT, 11);	
			$this->bindParam(2, $intOffset, PDO::PARAM_INT, 11);	
			$this->bindParam(3, $intLimit, PDO::PARAM_INT, 4);	
			$arrResult 	= $this->fetchAll();
			return isset($arrResult) ? $arrResult : null;
		} catch(Exception $e) {
			Vfriend_Function::logDb('News::getCmtList - ' .$e->getMessage());			
			return null;
		}
	}
	public function addComment($intUserId, $intNewsId, $strContent, $strQuote) {
		try {
			$this->prepare('call ncomments_add(?,?,?,?)');
			$this->bindParam(1, $intUserId, PDO::PARAM_INT, 11);
			$this->bindParam(2, $intNewsId, PDO::PARAM_INT, 11);
			$this->bindParam(3, $strContent);
			$this->bindParam(4, $strQuote);
			$arrResult	= $this->fetchAll();
			$intError	= isset($arrResult[0]['errCode']) ? $arrResult[0]['errCode'] : 0;
			if($intError == 0) {
				$intSubcategoryId 	= isset($arrResult[0]['subcategoryid']) ? $arrResult[0]['subcategoryid'] : 0;
				$intOwnerId 		= isset($arrResult[0]['ownerid']) ? $arrResult[0]['ownerid'] : 0;
				$intNcommentId		= isset($arrResult[0]['ncommentid']) ? $arrResult[0]['ncommentid'] : 0;
				$this->clear->deleteNews($intSubcategoryId, $intUserId);
				$this->clear->deleteNews($intSubcategoryId, $intOwnerId);
				$this->clear->deleteNewsById($intNewsId);
				$this->clear->deleteUstat($intUserId);
				$this->clear->deleteUstat($intOwnerId);
				# ---- LOGS ---- #
				$arrUFollow = $this->getFollowByNewId($intNewsId);					
				if($arrUFollow) {					
					$arrInfo	= $this->get($intNewsId);
					$strLogsContent = Zend_Json::encode(array(
														'newid'  	=> $intNewsId,
														'title'  	=> $arrInfo['title'],
														'posterid' 	=> $intUserId,
														'content'	=> $strContent
													)
												);
					foreach($arrUFollow as $value) {
						$intULogId = $value['userid'];
						if($intULogId != $intUserId) {
							$this->nlogs->insert($intULogId, 3, $intNcommentId, $intNewsId);
							$this->notify->insert($intULogId, 65, $strLogsContent, $intNcommentId);
						}
					}
				}
				// BEHAVIOUR
				$arrUser	= $this->getCmtUser($intNewsId);
				if($arrUser) {
					$isHasOwner = 0;					
					foreach($arrUser as $value) {
						$intReceiveId = $value['userid'];
						$this->logBehaviour($intReceiveId, $intOwnerId, $intUserId, 52, $intNewsId);						
					}				
				}
				$this->logBehaviour($intOwnerId, $intOwnerId, $intUserId, 52, $intNewsId);
				$this->logBehaviour($intUserId, $intOwnerId, $intUserId, 52, $intNewsId);
			}
			return $intError;
		} catch(Exception $e) {
			Vfriend_Function::logDb('News::addComment - ' .$e->getMessage());			
			return null;
		}
	}
	public function deleteComment($intUserId, $intNewsId, $intNcommentId) {
		try {
			$this->prepare('call ncomments_delete(?,?,?)');
			$this->bindParam(1, $intUserId, PDO::PARAM_INT, 11);
			$this->bindParam(2, $intNewsId, PDO::PARAM_INT, 11);
			$this->bindParam(3, $intNcommentId, PDO::PARAM_INT, 11);
			$arrResult 	= $this->fetchAll();
			$intError	= isset($arrResult[0]['errCode']) ? $arrResult[0]['errCode'] : 0;
			if($intError == 0) {
				$intSubcategoryId 	= isset($arrResult[0]['subcategoryid']) ? $arrResult[0]['subcategoryid'] : 0;
				$intOwnerId 		= isset($arrResult[0]['ownerid']) ? $arrResult[0]['ownerid'] : 0;
				$this->clear->deleteNews($intSubcategoryId, $intUserId);
				$this->clear->deleteNews($intSubcategoryId, $intOwnerId);
				$this->clear->deleteNewsById($intNewsId);				
				$this->nlogs->deleteByType(3, $intNcommentId);
				$this->nlogs->deleteByType(4, $intNcommentId);
				$this->notify->deleteByType(65, $intNcommentId);
				$this->notify->deleteByType(66, $intNcommentId);
			}
			return $intError;
		} catch(Exception $e) {
			Vfriend_Function::logDb('News::deleteComment - ' .$e->getMessage());			
			return null;
		}
	}
	public function getLike($intNewId, $intLimit=BEHAVIOUR_TOP_LIMIT) {
		try {
			$strKey	 	= NEWS_GETLIKE . $intNewId;
			$arrResult	= $this->cache->get($strKey);			
			if(!Vfriend_Function::isCached($arrResult)) {
				$this->prepare('call nlikes_get_top(?,?)');
				$this->bindParam(1, $intNewId, PDO::PARAM_INT, 11);	
				$this->bindParam(2, $intLimit, PDO::PARAM_INT, 4);	
				$arrResult 	= $this->fetchAll();
				$this->cache->set($strKey, $arrResult, NEWS_GETLIKE_EXPIRE);
			}
			return isset($arrResult) ? $arrResult : null;
		} catch(Exception $e) {
			Vfriend_Function::logDb('News::getLike - ' . $e->getMessage());
			return null;
		}
	}
	public function addLike($intUserId, $intNewsId, &$intLike=0) {
		try {
			$this->prepare('call nlikes_add(?,?)');
			$this->bindParam(1, $intUserId, PDO::PARAM_INT, 11);
			$this->bindParam(2, $intNewsId, PDO::PARAM_INT, 11);			
			$arrResult 	= $this->fetchAll();
			$intError 	= isset($arrResult[0]['errCode']) ? $arrResult[0]['errCode'] : 0;
			if($intError == 0) {
				$intLike 			= isset($arrResult[0]['tlike']) ? $arrResult[0]['tlike'] : 0;
				$intSubCategoryId 	= isset($arrResult[0]['subcategoryid']) ? $arrResult[0]['subcategoryid'] : 0;
				$intOwnerId 		= isset($arrResult[0]['ownerid']) ? $arrResult[0]['ownerid'] : 0;
				$this->clear->deleteNews($intSubCategoryId,$intUserId);
				$this->clear->deleteNews($intSubCategoryId,$intOwnerId);
				$this->clear->deleteNewsById($intNewsId);	
				$this->clear->deleteUstat($intUserId);
				# ---- LOGS ---- #
				$arrUFollow = $this->getFollowByNewId($intNewsId);
				if($arrUFollow) {
					$arrInfo		= $this->get($intNewsId);
					$strLogsContent = Zend_Json::encode(array(
														'newid'  	=> $intNewsId,
														'title'  	=> $arrInfo['title'],
														'posterid' 	=> $intUserId
													)
												);
					foreach($arrUFollow as $value) {
						$intULogId = $value['userid'];
						if($intULogId != $intUserId) {
							$this->nlogs->insert($intULogId, 1, $intNewsId, $intNewsId);
							$this->notify->insert($intULogId, 63, $strLogsContent, $intNewsId);
						}
					}
				}
			}
			return $intError;
		} catch(Exception $e) {
			Vfriend_Function::logDb('News::addLike - ' . $e->getMessage());
			return null;
		}
	}
	public function addLikeCmt($intUserId, $intNewsId, $intNcommentId, &$intLike=0) {
		try {
			$this->prepare('call nclikes_add(?,?,?)');
			$this->bindParam(1, $intUserId, PDO::PARAM_INT, 11);
			$this->bindParam(2, $intNewsId, PDO::PARAM_INT, 11);
			$this->bindParam(3, $intNcommentId, PDO::PARAM_INT, 11);
			$arrResult 	= $this->fetchAll();
			$intError 	= isset($arrResult[0]['errCode']) ? $arrResult[0]['errCode'] : 0;
			if($intError == 0) {
				$intLike 	= isset($arrResult[0]['tlike']) ? $arrResult[0]['tlike'] : 0;
				$intOwnerId = isset($arrResult[0]['userid']) ? $arrResult[0]['userid'] : 0;
				$this->clear->deleteUstat($intOwnerId);
				$this->clear->deleteUstat($intUserId);
				$this->clear->deleteNews($intUserId);
				# ---- LOGS ---- #
				$arrUFollow = $this->getFollowByNewId($intNewsId);
				if($arrUFollow) {
					$arrInfo		= $this->get($intNewsId);
					$strLogsContent = Zend_Json::encode(array(
														'newid'  	=> $intNewsId,
														'title'  	=> $arrInfo['title'],
														'posterid' 	=> $intUserId
													)
												);
					foreach($arrUFollow as $value) {
						$intULogId = $value['userid'];
						if($intULogId != $intUserId) {
							$this->nlogs->insert($intULogId, 4, $intNcommentId, $intNewsId);
							$this->notify->insert($intULogId, 66, $strLogsContent, $intNcommentId);
						}
					}
				}
			}
			return $intError;
		} catch(Exception $e) {
			Vfriend_Function::logDb('News::addLikeCmt - ' . $e->getMessage());
			return null;
		}
	}
	public function getFollow($intUserId, $intNewsId) {
		try {
			$strKey 	= NEWS_GETFOLLOW . $intUserId . $intNewsId;
			$arrResult	= $this->cache->get($strKey);
			if(!Vfriend_Function::isCached($arrResult)) {
				$this->prepare('call nfollows_get_check(?,?)');
				$this->bindParam(1, $intUserId, PDO::PARAM_INT, 11);
				$this->bindParam(2, $intNewsId, PDO::PARAM_INT, 11);			
				$arrResult = $this->fetchAll();
				$this->cache->set($strKey, $arrResult, NEWS_GETFOLLOW_EXPIRE);
			}
			return isset($arrResult[0]['nfollowid']) ? $arrResult[0]['nfollowid'] : 0;
		} catch(Exception $e) {
			Vfriend_Function::logDb('News::getFollow - ' . $e->getMessage());
			return null;
		}
	}
	public function getFollowByNewId($intNewsId) {
		try {
			$strKey 	= NEWS_GETFOLLOWBYNEWID . $intNewsId;
			$arrResult	= $this->cache->get($strKey);
			if(!Vfriend_Function::isCached($arrResult)) {
				$this->prepare('call nfollows_getall(?)');
				$this->bindParam(1, $intNewsId, PDO::PARAM_INT, 11);			
				$arrResult = $this->fetchAll();
				$this->cache->set($strKey, $arrResult, NEWS_GETFOLLOWBYNEWID_EXPIRE);
			}
			return isset($arrResult) ? $arrResult : null;
		} catch(Exception $e) {
			Vfriend_Function::logDb('News::getFollowByNewId - ' . $e->getMessage());
			return null;
		}
	}
	public function addFollow($intUserId, $intNewsId) {
		try {
			$this->prepare('call nfollows_add(?,?)');
			$this->bindParam(1, $intUserId, PDO::PARAM_INT, 11);
			$this->bindParam(2, $intNewsId, PDO::PARAM_INT, 11);			
			if($this->execute()) {
				$this->clear->deleteFollowForNews($intUserId, $intNewsId);				
				$this->nlogs->insert($intUserId, 0, $intNewsId, $intNewsId);
			}
		} catch(Exception $e) {
			Vfriend_Function::logDb('News::addFollow - ' . $e->getMessage());
			return null;
		}
	}
	public function deleteFollow($intUserId, $intNewsId) {
		try {
			$this->prepare('call nfollows_delete(?,?)');
			$this->bindParam(1, $intUserId, PDO::PARAM_INT, 11);
			$this->bindParam(2, $intNewsId, PDO::PARAM_INT, 11);			
			if($this->execute()) {
				$this->clear->deleteFollowForNews($intUserId, $intNewsId);
				$this->nlogs->deleteByUserId($intUserId, $intNewsId);
			}
		} catch(Exception $e) {
			Vfriend_Function::logDb('News::addFollow - ' . $e->getMessage());
			return null;
		}
	}
	public function updateImg($intUserId, $intNewsId,&$strImage='') {
		try {
			$this->prepare('call news_update_image(?,?)');
			$this->bindParam(1, $intUserId, PDO::PARAM_INT, 11);
			$this->bindParam(2, $intNewsId, PDO::PARAM_INT, 11);
			$arrResult 	= $this->fetchAll();
			$intError 	= isset($arrResult[0]['errCode']) ? $arrResult[0]['errCode'] : 0;
			if($intError == 0) {
				$strImage 	= isset($arrResult[0]['image']) ? $arrResult[0]['image'] : '';
			}
			return $intError;
		} catch(Exception $e) {
			Vfriend_Function::logDb('News::updateImg - ' . $e->getMessage());
			return null;
		}
	}
	public function getQuote($intNewsId, $intType) {
		try {
			$strKey	 	= NEWS_GETQUOTE . $intNewsId . $intType;
			$arrResult	= $this->cache->get($strKey);
			if(!Vfriend_Function::isCached($arrResult)) {
				$this->prepare('call news_get_quote(?,?)');
				$this->bindParam(1, $intNewsId, PDO::PARAM_INT, 11);	
				$this->bindParam(2, $intType, PDO::PARAM_INT, 4);	
				$arrResult 	= $this->fetchAll();
				$this->cache->set($strKey, $arrResult, NEWS_GETQUOTE_EXPIRE);
			}
			return (isset($arrResult[0]['ncommentid']) && $arrResult[0]['ncommentid'] > 0)  ? $arrResult[0] : null;
		} catch(Exception $e) {
			Vfriend_Function::logDb('News::getQuote - ' . $e->getMessage());
			return null;
		}
	}
	public function deleteImg($intNewsId, $strImg='') {
		if($strImg == '') {
			$arrRow = $this->get($intNewsId);
			if(isset($arrRow) && $arrRow) {
				if(isset($arrRow['image']) && $arrRow['image'] != '') {
					if(file_exists(DIR_NEWS_FULL . $arrRow['image']))
						@unlink(DIR_NEWS_FULL . $arrRow['image']);
					if(file_exists(DIR_NEWS_THUMB . $arrRow['image']))
						@unlink(DIR_NEWS_THUMB . $arrRow['image']);
					if(file_exists(DIR_NEWS_SMALL . $arrRow['image']))
						@unlink(DIR_NEWS_SMALL . $arrRow['image']);
				}
			}
		} else {
			if(file_exists(DIR_NEWS_FULL . $strImg))
				@unlink(DIR_NEWS_FULL . $strImg);
			if(file_exists(DIR_NEWS_THUMB . $strImg))
				@unlink(DIR_NEWS_THUMB . $strImg);
			if(file_exists(DIR_NEWS_SMALL . $strImg))
				@unlink(DIR_NEWS_SMALL . $strImg);
		}
	}
	public function logBehaviour($intUserId,$intOwnerId,$intPosterId,$intType,$intSrcId) {
		$strQueryCore 		= 'get';
		$strQueryLike		= 'getLike';
		$strQueryTotalCmt	= 'getCmtTotal';
		$strQueryCmt		= 'getTopCmt';		
		$strQueryQuote		= 'getQuote';
		$this->behaviour->insert($intUserId,
								 $intOwnerId,
								 $intPosterId,
								 $strQueryCore,
								 $strQueryLike,
								 $strQueryTotalCmt,
								 $strQueryCmt,
								 $strQueryQuote,
								 $intType,
								 $intSrcId);
	}
}
?>
